<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <!-- 仅仅提供思路,逻辑还是没有实现的 -->
  <script>

    //获得本地缓存的数据
    function getUploadedFromStorage() {
      return JSON.parse(localStorage.getItem(saveChunkKey) || "{}");
    }

    //写入缓存
    function setUploadedToStorage(index) {
      var obj = getUploadedFromStorage();
      obj[index] = true;
      localStorage.setItem(saveChunkKey, JSON.stringify(obj));
    }

    //分段对比

    var uploadedInfo = getUploadedFromStorage();//获得已上传的分段信息

    for (var i = 0; i < chunkCount; i++) {
      console.log('index', i, uploadedInfo[i] ? '已上传过' : '未上传');

      if (uploadedInfo[i]) {//对比分段
        sendChunkCount = i + 1;//记录已上传的索引
        continue;//如果已上传则跳过
      }
      var fd = new FormData();   //构造FormData对象
      fd.append('token', token);
      fd.append('f1', chunks[i]);
      fd.append('index', i);

      (function (index) {
        xhrSend(fd, function () {
          sendChunkCount += 1;
          //将成功信息保存到本地
          setUploadedToStorage(index);
          if (sendChunkCount === chunkCount) {
            console.log('上传完成，发送合并请求');
            var formD = new FormData();
            formD.append('type', 'merge');
            formD.append('token', token);
            formD.append('chunkCount', chunkCount);
            formD.append('filename', name);
            xhrSend(formD);
          }
        });
      })(i);
    }

  </script>
</body>

</html>